

package faac.http.MainPage;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import faac.Manager;
import faac.modules.chat.ChatCommandProc;
import faac.util.MainProps;

/**
 *
 * @author -D@@G-
 */
public class user_auth_props extends MainPageServletActions{



    /**
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @Override
    @SuppressWarnings("unchecked")
public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String ns = request.getParameter("ns"); // Имя сервиса
        if (!Manager.getInstance().getServiceNames().contains(ns)) {
            return "/main?page=error&id=0&ret=index";
        }
        String save = request.getParameter("save");
        String cmd = request.getParameter("cmd");
        if(save == null) {
print(response, HTML_HEAD + "<TITLE>JimBot "+ MainProps.VERSION+" </TITLE></HEAD>" + BODY +
                            "<H2>Панель управления ботом</H2>" +
                            "<H3>Управление полномочиями пользователей</H3>");
        String[] gr = Manager.getInstance().getService(ns).getProps().getStringProperty("auth.groups").split(";");
        Set<String> au = ((ChatCommandProc)Manager.getInstance().getService(ns).cmd).getAuthObjects().keySet();
        Map m = ((ChatCommandProc)Manager.getInstance().getService(ns).cmd).getAuthObjects();
        HashSet[] grs = new HashSet[gr.length];
        for(int i=0; i<gr.length; i++){
            grs[i] = new HashSet<String>();
            try {
                String[] s = Manager.getInstance().getService(ns).getProps().getStringProperty("auth.group_"+gr[i]).split(";");
                if(s.length>0) {
                        for (int j = 0; j < s.length; j++) {
                            grs[i].add(s[j]);
                        }
                    }
            } catch (Exception ex) {}
        }
        String s = "<FORM METHOD=POST ACTION=\"main" +
        "\"><INPUT TYPE=hidden NAME=\"page\" VALUE=\"user_auth_props_in\">" +
        "<INPUT TYPE=hidden NAME=\"ns\" VALUE=\"" +ns + "\">" +
        "<INPUT TYPE=hidden NAME=\"uid\" VALUE=\"" + "\">";
          s += "<TABLE><tbody><TH ALIGN=LEFT>";
        for(int i=0;i<gr.length;i++) {
                s += "<TD><b><u>" + gr[i] + "</u></b></TD>";
            }
        s += "</TR>";
        for(String ss:au){
//            s += "<TR style=\"background-color: rgb(255, 255, 255);\" " +
  //          		"onmouseover=\"this.style.backgroundColor='#ecece4'\" " +
   //         		"onmouseout=\"this.style.backgroundColor='#d9d9c8'\">" +
  s +=          		"<TH ALIGN=LEFT>" + m.get(ss) + "  [" + ss + "]</TD>";
            for(int i=0; i<gr.length; i++){
                s += "<TD><INPUT TYPE=CHECKBOX NAME=\"" + gr[i] + "_" + ss +
                    "\" VALUE=\"true\" " + (grs[i].contains(ss) ? "CHECKED" : "") + "></TD>";
            }
            s += "</TR>";
        }
        s += "</tbody></TABLE><P><INPUT TYPE=submit VALUE=\"Сохранить\"></FORM>";
        print(response,s);
      print(response, "<P><A HREF=\"?page=index\">" +
                            "Назад</A><br>");
        print(response, "</FONT></BODY></HTML>");
    }
        


        


        return null;
    }
}